package com.youkeyi.ddy.cloud.application.business.storage.category.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.BusinessCategoryCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.dto.BusinessCategoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.dto.BusinessCategoryListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.param.BusinessCategoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.param.BusinessCategoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.param.BusinessCategoryListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.storage.category.param.BusinessCategoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.category.StorageCategoryCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.category.dto.StorageCategoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.category.param.StorageCategoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.category.param.StorageCategoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.category.param.StorageCategoryQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.category.param.StorageCategoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessCategoryController
 *
 * @Author liuxiawang
 * @Date 2024/4/18 10:17 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessCategoryController implements BusinessCategoryCloudService {

    @Autowired
    private StorageCategoryCloudService storageCategoryCloudService;

    @Override
    public BusinessCategoryListCloudDto list(BusinessCategoryListCloudParam param) {
        BusinessCategoryListCloudDto dto = new BusinessCategoryListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        StorageCategoryQueryCloudParam categoryQueryCloudParam = new StorageCategoryQueryCloudParam();
        categoryQueryCloudParam.setCategoryPrimaryKeys(param.getCategoryPrimaryKeys());
        categoryQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        categoryQueryCloudParam.setCodes(param.getCodes());
        categoryQueryCloudParam.setCodeFuzzy(param.getCodeFuzzy());
        categoryQueryCloudParam.setNames(param.getNames());
        categoryQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        categoryQueryCloudParam.setLevel(param.getLevel());
        categoryQueryCloudParam.setParentKeys(param.getParentKeys());
        categoryQueryCloudParam.setPageSize(param.getPageSize());
        categoryQueryCloudParam.setCurrentPage(param.getCurrentPage());
        StorageCategoryListCloudDto categoryDtos = storageCategoryCloudService.query(categoryQueryCloudParam);
        if (null != categoryDtos && CollectionUtils.isNotEmpty(categoryDtos.getRows())) {
            List<BusinessCategoryListItemCloudDto> items = new ArrayList<>();
            categoryDtos.getRows().forEach(category -> {
                BusinessCategoryListItemCloudDto item = new BusinessCategoryListItemCloudDto();
                item.setCategoryPrimaryKey(category.getCategoryPrimaryKey());
                item.setName(category.getName());
                item.setCode(category.getCode());
                item.setPropertyKey(category.getPropertyKey());
                item.setLevel(category.getLevel());
                item.setParentKey(category.getParentKey());
                item.setParentName(category.getParentName());
                item.setDesc(category.getDesc());
                item.setRemark(category.getRemark());
                item.setIsDeleted(category.getIsDeleted());
                item.setIsDeletedName(category.getIsDeletedName());
                item.setCreateUserKey(category.getCreateUserKey());
                item.setCreateTime(category.getCreateTime());
                item.setUpdateUserKey(category.getUpdateUserKey());
                item.setUpdateTime(category.getUpdateTime());
                item.setCreateUserName(category.getCreateUserName());
                item.setUpdateUserName(category.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(categoryDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessCategoryCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageCategoryCreateCloudParam categoryCreateCloudParam = new StorageCategoryCreateCloudParam();
        categoryCreateCloudParam.setCorpKey(corpKey);
        categoryCreateCloudParam.setName(param.getName());
        categoryCreateCloudParam.setCode(param.getCode());
        categoryCreateCloudParam.setPropertyKey(param.getPropertyKey());
        categoryCreateCloudParam.setParentKey(param.getParentKey());
        categoryCreateCloudParam.setDesc(param.getDesc());
        categoryCreateCloudParam.setRemark(param.getRemark());
        categoryCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        categoryCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return storageCategoryCloudService.create(categoryCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessCategoryUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCategoryPrimaryKey())
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getCode())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageCategoryUpdateCloudParam categoryUpdateCloudParam = new StorageCategoryUpdateCloudParam();
        categoryUpdateCloudParam.setCategoryPrimaryKey(param.getCategoryPrimaryKey());
        categoryUpdateCloudParam.setName(param.getName());
        categoryUpdateCloudParam.setCode(param.getCode());
        categoryUpdateCloudParam.setParentKey(param.getParentKey());
        categoryUpdateCloudParam.setPropertyKey(param.getPropertyKey());
        categoryUpdateCloudParam.setDesc(param.getDesc());
        categoryUpdateCloudParam.setRemark(param.getRemark());
        categoryUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        categoryUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return storageCategoryCloudService.update(categoryUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessCategoryDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCategoryPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        StorageCategoryDeleteCloudParam categoryDeleteCloudParam = new StorageCategoryDeleteCloudParam();
        categoryDeleteCloudParam.setCategoryPrimaryKeys(param.getCategoryPrimaryKeys());
        categoryDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        categoryDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return storageCategoryCloudService.delete(categoryDeleteCloudParam);
    }
}
